package cn.com.bluemoon.daps.common.domain;

import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Objects;


/**
 * @author Jarod.Kong
 * @date 2020/10/16 15:50
 */
public class FeignApiRespBean<T> {
    /**
     * 响应代码
     */
    private String respCode;

    /**
     * 响应内容
     */
    private T respData;

    /**
     * 响应描述
     */
    private String respDesc;

    private String token;

    public void setToken(String token) {
        this.token = token;
    }

    public String getToken() {
        return token;
    }

    public T getRespData() {
        return (T) respData;
    }

    public String getRespCode() {
        return respCode;
    }

    public String getRespDesc() {
        return respDesc;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }

    public void setRespData(T respData) {
        this.respData = respData;
    }

    public void setRespDesc(String respDesc) {
        this.respDesc = respDesc;
    }

    /**
     * feign返回转未this
     *
     * @param feignApiRespBean this
     * @param <T> p
     * @return base
     */
    public static <T> ResultBean<T> of(FeignApiRespBean<T> feignApiRespBean) {
        if (feignApiRespBean == null){
            return ResultBean.error("登录门户服务失败，不返回登录认知信息");
        }
        int code = -1;
        if (NumberUtil.isNumber(feignApiRespBean.getRespCode())) {
            try {
                code = NumberUtil.parseInt(feignApiRespBean.getRespCode());
            } catch (NumberFormatException e) {
                // nothing
            }
        }
        // feign的100对应200正常
        if (code == 100) {
            code = 200;
        }
        return ResultBean.data(code,feignApiRespBean.getRespData()).setMsg(feignApiRespBean.getRespDesc());
    }

    public boolean isOk(){
        return Objects.equals(respCode, "100");
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
